package com.mission.schedule.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.mission.schedule.R;
import com.mission.schedule.annotation.ViewResId;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.qd606.NoteTitleDetailBean;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.utils.DateUtil;
import com.mission.schedule.utils.SharedPrefUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by liwei on 2017/3/15.
 */

public class NoteListActivity extends BaseActivity {

    @ViewResId(id = R.id.top_ll_back)
    LinearLayout top_ll_back;
    @ViewResId(id = R.id.listview)
    ListView listView;

    List<NoteTitleDetailBean.TDelListBean> tDelListBeanList = new ArrayList<>();

    Apdater apdater;

    String userID = "";
    String content = "";
    String maxTitleId = "";

    @Override
    protected void setListener() {
        top_ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.note_list);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

        SharedPrefUtil sharedPrefUtil = new SharedPrefUtil(this, ShareFile.USERFILE);
        userID = sharedPrefUtil.getString(this, ShareFile.USERFILE,
                ShareFile.USERID, "0");

        View view = LayoutInflater.from(this).inflate(R.layout.emptry_headerview, null);
        listView.addHeaderView(view);
        listView.addFooterView(view);

        content = getIntent().getStringExtra("content");
        maxTitleId = getIntent().getStringExtra("maxTitleId");
        updateNoteTitlesData();

    }

    @Override
    protected void setAdapter() {

    }

    private void updateNoteTitlesData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                tDelListBeanList.clear();
                tDelListBeanList = App.getDBcApplication().getAllNoteTitlesData(userID, false);

                if (tDelListBeanList != null) {
                    if (tDelListBeanList.size() > 0) {
                        NoteTitleDetailBean.TDelListBean tDelListBean = tDelListBeanList.get(0);
                        tDelListBeanList.remove(0);
                        if (tDelListBeanList.size() > 0) {
                            Collections.sort(tDelListBeanList, new Comparator<NoteTitleDetailBean.TDelListBean>() {
                                @Override
                                public int compare(NoteTitleDetailBean.TDelListBean lhs, NoteTitleDetailBean.TDelListBean rhs) {
                                    return rhs.localTimes.compareTo(lhs.localTimes);
                                }
                            });
                            tDelListBeanList.add(0, tDelListBean);
                        } else {
                            tDelListBeanList.add(tDelListBean);
                        }

                        for (NoteTitleDetailBean.TDelListBean bean : tDelListBeanList) {
                            if (bean.titleId.equals(maxTitleId)) {
                                tDelListBeanList.remove(bean);
                                break;
                            }
                        }
                    }
                }


                apdater = new Apdater();
                listView.setAdapter(apdater);

            }
        }).start();

    }


    class Apdater extends BaseAdapter {

        @Override
        public int getCount() {
            return tDelListBeanList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(NoteListActivity.this).inflate(R.layout.note_list_item, null);
                holder.icon = (ImageView) convertView.findViewById(R.id.icon);
                holder.title = (TextView) convertView.findViewById(R.id.title);
                holder.ll = (LinearLayout) convertView.findViewById(R.id.ll);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            switch (tDelListBeanList.get(position).ltype) {
                case 0:
                    holder.icon.setBackgroundResource(R.mipmap.icon_beiwang);
                    break;
                case 1:
                    holder.icon.setBackgroundResource(R.mipmap.icon_putong);
                    break;
                case 2:
                    holder.icon.setBackgroundResource(R.mipmap.icon_buy);
                    break;
                case 3:
                    holder.icon.setBackgroundResource(R.mipmap.icon_xinyuan);
                    break;
                case 4:
                    holder.icon.setBackgroundResource(R.mipmap.icon_renwu);
                    break;
            }
            final int num = App.getDBcApplication().getTitleNoteDetailData(tDelListBeanList.get(position).uid + "", tDelListBeanList.get(position).titleId, false).size();
            String titlestr = tDelListBeanList.get(position).titles;
            String nums = "";
            if (num == 0) {
                if (tDelListBeanList.get(position).titleId.equals("0")) {
                    titlestr = "<font color='#30a9a0'>" + titlestr + "</>";
                } else {
                    titlestr = "<font color='#000000'>" + titlestr + "</>";
                }
                holder.title.setText(Html.fromHtml(titlestr));
            } else {
                if (tDelListBeanList.get(position).styles == 0) {
                    int tick = App.getDBcApplication().getNoteDetailEndstateNums(tDelListBeanList.get(position).titleId, tDelListBeanList.get(position).uid + "");
                    if (num == 0) {
                        nums = "<font color='#aaaaaa'>(" + tick + "/" + num + ")</>";
                    } else {
                        nums = "";
                    }

                    if (tDelListBeanList.get(position).titleId.equals("0")) {
                        titlestr = "<font color='#30a9a0'>" + titlestr + "</><font color='#aaaaaa'>(" + num + ")</>";
                        holder.title.setText(Html.fromHtml(titlestr + nums));
                    } else {
                        if (num == 0) {
                            setcolor(titlestr, holder.title);
                        } else {
                            setcolor(titlestr + "(" + tick + "/" + num + ")", holder.title);
                        }
                    }
                } else {
                    if (num == 0) {
                        nums = "<font color='#aaaaaa'>(" + num + ")</>";
                    } else {
                        nums = "";
                    }
                    if (tDelListBeanList.get(position).titleId.equals("0")) {
                        titlestr = "<font color='#30a9a0'>" + titlestr + "</><font color='#aaaaaa'>(" + num + ")</>";
                        holder.title.setText(Html.fromHtml(titlestr + nums));
                    } else {
                        if (num == 0) {
                            setcolor(titlestr, holder.title);
                        } else {
                            setcolor(titlestr + "(" + num + ")", holder.title);
                        }
                    }
                }
            }

            holder.ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    NoteTitleDetailBean.ListBean listBean = new NoteTitleDetailBean.ListBean();
                    listBean.titleId = tDelListBeanList.get(position).titleId;
                    listBean.uid = Integer.valueOf(userID);
                    listBean.id = App.getDBcApplication().getMinId() - 1;
                    listBean.style = tDelListBeanList.get(position).styles;
                    listBean.changetime = DateUtil.nowTime();
                    listBean.createTime = DateUtil.nowTime();
                    listBean.lType = tDelListBeanList.get(position).ltype;
                    listBean.orderId = num + 1;
                    listBean.endstate = 0;
                    listBean.remark = "";
                    listBean.contents = content;
                    listBean.titles = tDelListBeanList.get(position).titles;
                    listBean.imgPath = "";
                    listBean.imgUrl = "";
                    listBean.cpath = "";
                    listBean.curl = "";
                    listBean.nums = "";
                    listBean.shareUrl = "";
                    listBean.remark1 = "";
                    listBean.remark2 = "";
                    listBean.remark3 = "";
                    listBean.remark4 = "";

                    App.getDBcApplication().saveNoteDetailData(listBean);
                    App.getDBcApplication().updateNoteTitleDetailState(listBean.titleId, listBean.id, userID, 1, false);


                    Intent intent = new Intent(NoteListActivity.this, NoteEditActivity.class);
                    NoteListActivity.this.setResult(1, intent);
                    finish();
                }
            });

            return convertView;
        }

        class ViewHolder {
            TextView title;
            ImageView icon;
            LinearLayout ll;
        }


        //设置双#号包裹的文字颜色
        private void setcolor(String content, TextView editText) {
            SpannableStringBuilder builder = new SpannableStringBuilder(content);
            if (content.length() > 1) {
                List<Integer> integerList = new ArrayList<>();
                int index = 0;
                int count = hit(content, "#");
                if (count > 1) {
                    if (count % 2 == 0) {
                        for (int i = 0; i < count; i++) {
                            if (i == 0) {
                                index = content.indexOf("#");
                            } else {
                                index = content.indexOf("#", index + 1);
                            }
                            integerList.add(index);
                        }
                        for (int i = 0; i < integerList.size(); i++) {
                            if (i > 0) {
                                if (i % 2 != 0) {
                                    ForegroundColorSpan span = new ForegroundColorSpan(Color.BLACK);
                                    builder.setSpan(span, integerList.get(i - 1), integerList.get(i) + 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                                }
                            }
                        }
                        ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#aaaaaa"));
                        int khindex = content.indexOf("(");
                        builder.setSpan(span, khindex, content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        editText.setText(builder);
                    } else {
                        for (int i = 0; i < count - 1; i++) {
                            if (i == 0) {
                                index = content.indexOf("#");
                            } else {
                                index = content.indexOf("#", index + 1);
                            }
                            integerList.add(index);

                        }
                        for (int i = 0; i < integerList.size(); i++) {
                            if (i > 0) {
                                if (i % 2 != 0) {
                                    ForegroundColorSpan span = new ForegroundColorSpan(Color.BLACK);
                                    builder.setSpan(span, integerList.get(i - 1), integerList.get(i) + 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                                }
                            }
                        }
                        ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#aaaaaa"));
                        int khindex = content.indexOf("(");
                        builder.setSpan(span, khindex, content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        editText.setText(builder);
                    }
                } else {
                    ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#aaaaaa"));
                    int khindex = content.indexOf("(");
                    builder.setSpan(span, khindex, content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    editText.setText(builder);
                }
            } else {
                ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#aaaaaa"));
                int khindex = content.indexOf("(");
                builder.setSpan(span, khindex, content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                editText.setText(builder);
            }
        }

        public int hit(String a, String b) {
            if (a.length() < b.length()) {
                return 0;
            }
            char[] a_t = a.toCharArray();
            int count = 0;

            for (int i = 0; i < a.length(); i++) {
                StringBuffer buffer = new StringBuffer();
                for (int j = 0; j < b.length(); j++) {
                    buffer.append(a_t[i + j]);
                }
                if (buffer.toString().equals(b)) {
                    count++;
                }
            }

            return count;
        }
    }
}
